﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ivony.Html;
using Ivony.Html.Parser;

namespace WindowsFormsApp1
{
	public partial class kapian : Form
	{
		public kapian()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < 100000; i += 50)
			{
				int c = i;
			}

		}
			private bool GarpStart()
		{
			/*	string postString = "";//这里即为传递的参数，可以用工具抓包分析，也可以自己分析，主要是form里面每一个name都要加进来  
				for (int i = 0; i < 100000; i+=50)
				{
					//postString = "act=submit&data_type=0&RenYue=0&year=" + item.Year + "&month=" + item.Month + "&day=" + item.Day + "&hour=" + item.Hour + "&minute=30&zty=0&sex=" + item.Sex + "&name=海马";
					byte[] postData = Encoding.UTF8.GetBytes(postString);//编码，尤其是汉字，事先要看下抓取网页的编码方式  
					string url = "https://www.kronozio.com/search?SortBy=pd&Category=Baseball&Ebay=1&PaginationSkip="+i;//地址  
					WebClient webClient = new WebClient();
					webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header，如果改为GET方式的话就去掉这句话即可  
					byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流  
					string srcString = Encoding.UTF8.GetString(responseData);//解码  

					var result = _bll.GrapBz(srcString, item);
					if (result)
					{
						return GarpMainDao.UpdateStates(item.Id, 1);
					}
					else
					{
						num = 0;
						Application.Exit();
					}
				}*/
			return true;
		}

		private void button2_Click(object sender, EventArgs e)
		{//12
			for (int i = 0; i < 100000; i+=50)
			{ 
				var request = (HttpWebRequest)WebRequest.Create("https://www.kronozio.com/search?SortBy=pd&Category=Baseball&Ebay=1&PaginationSkip="+i);
				var response = (HttpWebResponse)request.GetResponse();
				var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
				dateHtml(responseString,i);
			}


		}

		private JumonyParser jp = new JumonyParser();

		public void dateHtml(string html,int index)                                                                                                                                                                                                                                                               
		{
			string urls = "https://kronozio.blob.core.windows.net/images/card/";
			string url = "";
			string savepath = @"D:\imgDate_no";

			var dataDocument = jp.Parse(html);
			var mumberlist = dataDocument.Find(".SRCard").ToArray();
			for (int i = 0; i < mumberlist.Length; i++)
			{
				var alllist = mumberlist[i].Find(".ca_aut").ToArray();
				var name = "";
				if (alllist.Length ==0)
				{
					//判断是否有签名
					//int d = alllist[0].ToString().IndexOf("Autograph");
					//if (d == 0)
					//{
						//https://kronozio.blob.core.windows.net/images/card/9efbb95c9a04485e90961126f49409cb_front.jpg
						//https://kronozio.blob.core.windows.net/images/card/9efbb95c9a04485e90961126f49409cb_front.jpg
						var imguls = mumberlist[i].Find(".SRImgDiv").ToArray();
						string idcard = mumberlist[i].Attribute("idcard").Value().Replace("-", "");
						//获取但单张图片
						foreach (var imgli in imguls)
						{
							var imgsa = imgli.Find("img").ToArray();
							foreach (var img in imgsa)
							{
								var src = img.Attribute("src").Value(); 
							    name = img.Attribute("alt").Value();
								if (!string.IsNullOrWhiteSpace(name))
								{
									string filename = DownloadImage(urls + idcard + "_front" + ".jpg", savepath + "\\" + index + "\\", name + "_front");
									DownloadImage(urls + idcard + "_back" + ".jpg", savepath + "\\" + index + "\\", name + "_back");
								}
							} 
						} 

				//	}  
				}
			}
		}

		/// 网络地址
		/// 本地保存路径
		public static string DownloadImage(string url, string path,string name)
		{
			HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
			req.ServicePoint.Expect100Continue = false;
			req.Method = "GET";
			req.KeepAlive = true;
			req.ContentType = "image/*";
			HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
			System.IO.Stream stream = null;
			int i = url.LastIndexOf(".");
			string StrType = url.Substring(i);
			//string filename = Guid.NewGuid().ToString();
			try
			{
				CreateDirectory(path);
				// 以字符流的方式读取HTTP响应
				stream = rsp.GetResponseStream();
				Image.FromStream(stream).Save(path + "\\" + name.Replace(@"\", "").Replace(@"/", "") + StrType);
			}
			catch
			{

			}
			finally
			{
				// 释放资源
				if (stream != null) stream.Close();
				if (rsp != null) rsp.Close();
			}
			return name + StrType;
		}


		/// 网络地址
		/// 本地保存路径
		public static string DownloadImages(string url, string path, string name)
		{
			HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
			req.ServicePoint.Expect100Continue = false;
			req.Method = "GET";
			req.KeepAlive = true;
			req.ContentType = "image/*";
			HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
			System.IO.Stream stream = null;
			int i = url.LastIndexOf(".");
			string StrType = url.Substring(i);
			string filename = Guid.NewGuid().ToString();
			try
			{
				CreateDirectory(path);
				// 以字符流的方式读取HTTP响应
				stream = rsp.GetResponseStream();
				Image.FromStream(stream).Save(path + "\\"  + name);
			}
			finally
			{
				// 释放资源
				if (stream != null) stream.Close();
				if (rsp != null) rsp.Close();
			}
			return filename + StrType;
		}


		#region 创建一个目录  
		/// <summary>  
		/// 创建一个目录  
		/// </summary>  
		/// <param name="directoryPath">目录的绝对路径</param>  
		public static void CreateDirectory(string directoryPath)
		{
			//如果目录不存在则创建该目录  
			if (!IsExistDirectory(directoryPath))
			{
				Directory.CreateDirectory(directoryPath);
			}
		}
		#endregion

		#region 检测指定目录是否存在  
		/// <summary>  
		/// 检测指定目录是否存在  
		/// </summary>  
		/// <param name="directoryPath">目录的绝对路径</param>          
		public static bool IsExistDirectory(string directoryPath)
		{
			return Directory.Exists(directoryPath);
		}
		#endregion

		private void button1_Click_1(object sender, EventArgs e)
		{
			for (int i = 0; i < 100000; i += 50)
			{
				int c = i;
			}

		}
	}
}
